//-----------------------------------------------------------------------
// <copyright file="DirectorDAOBase.cs" company="FetCo">
//     Copyright (c) FetCo. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using FetCo.Framework;
using FetCo.Framework.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using ADESSA.Entities;

namespace ADESSA.Data
{    
    public abstract class DirectorDAOBase
    {
        #region Common methods
        public virtual Director CreateDirectorFromReader(IDataReader reader)
        {
            Director item = new Director();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("DirectorId"))) item.DirectorId = (long)reader["DirectorId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Url"))) item.Url = (string)reader["Url"];
                if (!reader.IsDBNull(reader.GetOrdinal("Title"))) item.Title = (string)reader["Title"];
                if (!reader.IsDBNull(reader.GetOrdinal("Summary"))) item.Summary = (string)reader["Summary"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) item.CreatedDate = (DateTime)reader["CreatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) item.UpdatedDate = (DateTime)reader["UpdatedDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("MemberId"))) item.MemberId = (long)reader["MemberId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Order"))) item.Order = (long)reader["Order"];
                if (!reader.IsDBNull(reader.GetOrdinal("CateArticleID"))) item.CateArticleID = (long)reader["CateArticleID"];
                if (!reader.IsDBNull(reader.GetOrdinal("Viewed"))) item.Viewed = (long)reader["Viewed"];
                if (!reader.IsDBNull(reader.GetOrdinal("Image"))) item.Image = (string)reader["Image"];
                if (!reader.IsDBNull(reader.GetOrdinal("Imagethumbnail"))) item.Imagethumbnail = (string)reader["Imagethumbnail"];
                if (!reader.IsDBNull(reader.GetOrdinal("StatusId"))) item.StatusId = (int)reader["StatusId"];
                if (!reader.IsDBNull(reader.GetOrdinal("TGKM_ObjectId"))) item.TGKM_ObjectId = (long)reader["TGKM_ObjectId"];
                if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) item.StartDate = (DateTime)reader["StartDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("EndDate"))) item.EndDate = (DateTime)reader["EndDate"];
                if (!reader.IsDBNull(reader.GetOrdinal("Enable"))) item.Enable = (bool)reader["Enable"];
                if (!reader.IsDBNull(reader.GetOrdinal("ActivateCode"))) item.ActivateCode = (string)reader["ActivateCode"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateDirectorFromReaderException, ex);
            }
            return item;
        }
        #endregion
        
        #region CreateDirector methods
            
        public virtual void CreateDirector(Director director)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorInsert");
                
                database.AddInParameter(dbCommand, "@Url", DbType.String, director.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, director.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, director.Summary);
                database.AddInParameter(dbCommand, "@Description", DbType.String, director.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, director.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, director.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, director.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, director.Order);
                database.AddInParameter(dbCommand, "@CateArticleID", DbType.Int64, director.CateArticleID);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, director.Viewed);
                database.AddInParameter(dbCommand, "@Image", DbType.String, director.Image);
                database.AddInParameter(dbCommand, "@Imagethumbnail", DbType.String, director.Imagethumbnail);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, director.StatusId);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, director.TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, director.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, director.EndDate);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, director.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, director.ActivateCode);
                database.AddOutParameter(dbCommand, "@DirectorId", DbType.Int64, 8);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                director.DirectorId = (long)database.GetParameterValue(dbCommand, "@DirectorId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessCreateDirectorException, ex);
            }
        }

        #endregion

        #region UpdateDirector methods
        
        public virtual void UpdateDirector(Director director)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorUpdate");            
                
                database.AddInParameter(dbCommand, "@DirectorId", DbType.Int64, director.DirectorId);
                database.AddInParameter(dbCommand, "@Url", DbType.String, director.Url);
                database.AddInParameter(dbCommand, "@Title", DbType.String, director.Title);
                database.AddInParameter(dbCommand, "@Summary", DbType.String, director.Summary);
                database.AddInParameter(dbCommand, "@Description", DbType.String, director.Description);
                database.AddInParameter(dbCommand, "@CreatedDate", DbType.DateTime, director.CreatedDate);
                database.AddInParameter(dbCommand, "@UpdatedDate", DbType.DateTime, director.UpdatedDate);
                database.AddInParameter(dbCommand, "@MemberId", DbType.Int64, director.MemberId);
                database.AddInParameter(dbCommand, "@Order", DbType.Int64, director.Order);
                database.AddInParameter(dbCommand, "@CateArticleID", DbType.Int64, director.CateArticleID);
                database.AddInParameter(dbCommand, "@Viewed", DbType.Int64, director.Viewed);
                database.AddInParameter(dbCommand, "@Image", DbType.String, director.Image);
                database.AddInParameter(dbCommand, "@Imagethumbnail", DbType.String, director.Imagethumbnail);
                database.AddInParameter(dbCommand, "@StatusId", DbType.Int32, director.StatusId);
                database.AddInParameter(dbCommand, "@TGKM_ObjectId", DbType.Int64, director.TGKM_ObjectId);
                database.AddInParameter(dbCommand, "@StartDate", DbType.DateTime, director.StartDate);
                database.AddInParameter(dbCommand, "@EndDate", DbType.DateTime, director.EndDate);
                database.AddInParameter(dbCommand, "@Enable", DbType.Boolean, director.Enable);
                database.AddInParameter(dbCommand, "@ActivateCode", DbType.String, director.ActivateCode);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessUpdateDirectorException, ex);
            }
        }
        
        #endregion

        #region DeleteDirector methods
        public virtual void DeleteDirector(long directorId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorDelete");
                
                database.AddInParameter(dbCommand, "@DirectorId", DbType.Int64, directorId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessDeleteDirectorException, ex);
            }
        }

        #endregion

        #region GetDirector methods
        
        public virtual Director GetDirector(long directorId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorGet");
                
                database.AddInParameter(dbCommand, "@DirectorId", DbType.Int64, directorId);
                
                Director director =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        director = CreateDirectorFromReader(reader);
                        reader.Close();
                    }
                }
                return director;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetDirectorException, ex);
            }
        }
        
        #endregion

        #region GetDirectorList methods
        public virtual DirectorCollection GetDirectorList(DirectorColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                DirectorCollection directorCollection = new DirectorCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Director director = CreateDirectorFromReader(reader);
                        directorCollection.Add(director);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return directorCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetDirectorListException, ex);
            }
        }
        
        public virtual DirectorCollection GetDirectorList(DirectorColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetDirectorList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
		  #region GetDirectorList methods
        public virtual DirectorCollection GetDirectorListByParam(object orderBy, OrderDirection orderDirection, int page, int pageSize,string Criteria, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spDirectorGetListByParam");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
				database.AddInParameter(dbCommand, "@Criteria",  DbType.String, Criteria);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                DirectorCollection directorCollection = new DirectorCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Director director = CreateDirectorFromReader(reader);
                        directorCollection.Add(director);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return directorCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SR.DataAccessGetDirectorListException, ex);
            }
        }
        
        public virtual DirectorCollection GetDirectorListByParam(object orderBy, OrderDirection orderDirection,string Criteria)
        {            
            int totalRecords = 0;
            return GetDirectorListByParam(orderBy, orderDirection, 0, 0,Criteria, out totalRecords);
        }
        
        #endregion
    }
}